﻿/**********************************天使物流*******************************************
* 功能描述:系统设置——角色管理
* 作者:孟帅龙
* 时间:2022/1/12
* 
**********************************天使物流*******************************************/
using Snowflake.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TianShiProject.EntityFrameworkCore;
using TianShiProject.Models.Common;
using TianShiProject.Models.System.Roles;
using Volo.Abp.EntityFrameworkCore;

namespace TianShiProject.System.Roles
{
    public class RoleRepository : DapperDbContextRepository, IRoleRepository
    {
        public RoleRepository(IDbContextProvider<TianShiProjectDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        #region 角色管理——查询分页显示
        /// <summary>
        /// 角色管理——查询分页显示
        /// </summary>
        /// <param name="RoleName"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public async Task<PageResponse<RolesDto>> GetRolePageShow(string RoleName, int pageindex=1, int pagesize=3)
        {
            PageResponse<RolesDto> response = new PageResponse<RolesDto>();
            var sql = "select Id,RoleNumber,RoleName,Alias,CreateTime,SuperiorRole,IsDelete from Role where IsDelete=0";
            if (!string.IsNullOrWhiteSpace(RoleName))
            {
                sql += $" and RoleName like '%" + RoleName + "%'";
            }
            var list = await this.QueryAsync<RolesDto>(sql, null);
            response.PageTotal = list.Count;
            sql += $"order by Id offset {(pageindex - 1) * pagesize} rows fetch next {pagesize} rows only";
            var lists = await this.QueryAsync<RolesDto>(sql, null);
            response.PageData = lists;
            return response;
        }
        #endregion

        #region 角色管理——逻辑删除
        /// <summary>
        /// 角色管理——逻辑删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<PageResponse<RolesDto>> DeleteRole(int id)
        {
            PageResponse<RolesDto> response = new PageResponse<RolesDto>();
            var sql = $"update Role set IsDelete=1 where Id={id}";
            response.Code = await ExecuteAsync(sql, null);
            return response;
        }
        #endregion

        #region 角色管理——批量删除
        /// <summary>
        /// 角色管理——批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<PageResponse<RolesDto>> DeleteBatchRole(string ids)
        {
            PageResponse<RolesDto> response = new PageResponse<RolesDto>();
            var id = ids.Split(',');
            foreach (var item in id)
            {
                var sql = $"update Role set IsDelete=1 where Id={item}";
                response.Code = await ExecuteAsync(sql, null);
            }
            return response;
        }
        #endregion

        #region 角色管理——添加
        /// <summary>
        /// 角色管理——添加
        /// </summary>
        /// <param name="rolesDto"></param>
        /// <returns></returns>
        public async Task<int> PostAddRole(RolesDto rolesDto)
        {
            var worker = new IdWorker(1, 1);
            var id = worker.NextId().ToString();
            var sql = $"insert into Role values('{id}','{rolesDto.RoleName}','{rolesDto.Alias}',getdate(),'{rolesDto.SuperiorRole}',0)";
            return await ExecuteAsync(sql, rolesDto);
        }
        #endregion

        #region 角色管理——编辑
        /// <summary>
        /// 角色管理——编辑
        /// </summary>
        /// <param name="rolesDto"></param>
        /// <returns></returns>
        public async Task<int> PostUpdateRole(RolesDto rolesDto)
        {
            var sql = $"update Role set RoleNumber='{rolesDto.RoleNumber}',RoleName='{rolesDto.RoleName}',Alias='{rolesDto.Alias}',CreateTime=getdate(),SuperiorRole='{rolesDto.SuperiorRole}',IsDelete=0 where Id='{rolesDto.Id}'";
            return await ExecuteAsync(sql, rolesDto);
        } 
        #endregion
    }
}
